/* eslint-disable no-unused-vars */
import mockcolumn from './mockcolumn.json'

/**
 * 构建传递给后端的数据结构
 * @param targetArr 添加到右侧的表格数据
 * @returns  Array<{workorderId: string, workstageIds: Array<string>, columns: Array<string>}>
 */
function generatePostData(targetArr) {
  let temp = {} // 工单id去重对象，键为工单ID，值为新数组的索引
  let arr = []
  targetArr.map(t => {
    let tempProp = t.workorderId
    if (!temp.hasOwnProperty(t.workorderId)) {
      // 将新数组的索引存入temp对象，并在新数组中插入新的工单id对象
      temp[tempProp] = arr.length
      arr.push({workorderId: t.workorderId, workstageIds: [], columns: []})
    }
    let indexOfArr = temp[tempProp]
    arr[indexOfArr].workstageIds.push(t.workstageId)
    arr[indexOfArr].columns.push(`${t.sort}/${t.workstageName}/${t.name}/${t.remarks}`)
  })
  return arr
}

const utils = {generatePostData}

export default utils
